/*
 * @Descripttion:
 * @version:
 * @Author: sueRimn
 * @Date: 2020-10-19 17:08:12
 * @LastEditors: sueRimn
 * @LastEditTime: 2020-11-26 08:59:17
 */

/* 时间戳(仅显示年月日、时分秒) */
function getFormatTime(stamp, hasHMS = true) {
    let stampLength = stamp.toString().length;
    let tstamp;
    if (stampLength == 10) {
        tstamp = new Date(stamp * 1000);
    } else if (stampLength == 13) {
        tstamp = new Date(stamp);
    } else {
        return;
    }

    let year = tstamp.getFullYear(tstamp);
    let month = tstamp.getMonth(tstamp) + 1;
    let day = tstamp.getDate(tstamp);
    let hour = tstamp.getHours(tstamp);
    let minute = tstamp.getMinutes(tstamp);
    let second = tstamp.getSeconds(tstamp);
    if (month < 10) {
        month = "0" + month;
    }
    if (day < 10) {
        day = "0" + day;
    }
    if (hour < 10) {
        hour = "0" + hour;
    }
    if (minute < 10) {
        minute = "0" + minute;
    }
    if (second < 10) {
        second = "0" + second;
    }
    if (hasHMS) {
        return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    } else {
        return `${year}-${month}-${day}`;
    }
}

/**
 *获取连个时间段的日期
 * @param {Date} start yyyy-mm-dd
 * @return {Date} end yyyy-mm-dd
 */
function getBetweenDateStr(start, end) {
    var result = [];
    var beginDay = start.split("-");
    var endDay = end.split("-");
    var diffDay = new Date();
    var dateList = new Array();
    var i = 0;
    diffDay.setDate(beginDay[2]);
    diffDay.setMonth(beginDay[1] - 1);
    diffDay.setFullYear(beginDay[0]);
    result.push(start);
    while (i == 0) {
        var countDay = diffDay.getTime() + 24 * 60 * 60 * 1000;
        diffDay.setTime(countDay);
        dateList[2] = diffDay.getDate();
        dateList[1] = diffDay.getMonth() + 1;
        dateList[0] = diffDay.getFullYear();
        if (String(dateList[1]).length == 1) {
            dateList[1] = "0" + dateList[1];
        }
        if (String(dateList[2]).length == 1) {
            dateList[2] = "0" + dateList[2];
        }
        result.push(dateList[0] + "-" + dateList[1] + "-" + dateList[2]);
        if (
            dateList[0] == endDay[0] &&
            dateList[1] == endDay[1] &&
            dateList[2] == endDay[2]
        ) {
            i = 1;
        }
    }
    return result;
}
function getBriefTime(stime) {
    let stimeLength = stime.toString().length;
    let tstime;
    if (stimeLength == 10) {
        tstime = new Date(stime * 1000);
    } else if (stimeLength == 13) {
        tstime = new Date(stime);
    } else {
        return;
    }

    let year = tstime.getFullYear(tstime);
    let month = tstime.getMonth(tstime) + 1;
    let day = tstime.getDate(tstime);
    let hour = tstime.getHours(tstime);
    let minute = tstime.getMinutes(tstime);
    if (month < 10) {
        month = "0" + month;
    }
    if (day < 10) {
        day = "0" + day;
    }
    if (hour < 10) {
        hour = "0" + hour;
    }
    if (minute < 10) {
        minute = "0" + minute;
    }
    return `${year}-${month}-${day} ${hour}:${minute}`;
}

export { getBetweenDateStr, getFormatTime, getBriefTime };
